package Starter;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import smash_tris.Player;
import smash_tris.Room;

import Components.ImageButton;
import Connector.Connector;

public class LoginPane extends JPanel {

	/* Elemęty okna logowania */
	private JLabel login;
	private JLabel ipName;
	private ImageButton start;
	private ImageButton cancel;
	private JTextField logName;
	private JTextField ipAddress;
	private JFrame currentDialog;

	private Image background;
	private MainFrame mainFrame = null;

	/* Folder */
	private String DIR = "picture/logon_pic/";

	private Connector connector = null;

	public LoginPane(MainFrame mainFrame) {
		// this.connector = connector;
		this.mainFrame = mainFrame;
		try {
			background = ImageIO.read(new File(DIR + "beth_tetris.jpg"));
		} catch (IOException e) {
			System.out.println("Nie za�adowano t�a");
			e.printStackTrace();
		}
		login = new JLabel();
		ipName = new JLabel("Podaj adres IP");
		logName = new JTextField("Nickname");
		ipAddress = new JTextField("127.0.0.1");

		cancel = new ImageButton(DIR + "exit_UP.png", DIR + "exit_DOWN.png");
		start = new ImageButton(DIR + "start_UP.png", DIR + "start_DOWN.png");

		login.setText("Podaj nick: ");

		start.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					LoginPane.this.connector = new Connector(
							LoginPane.this.ipAddress.getText());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				LoginPane.this.mainFrame
						.setBroadcastListener(new BroadcastListener(
								LoginPane.this.connector.getBroadCastIn()));
				LoginPane.this.mainFrame.setConnector(LoginPane.this.connector);
				

				Player player = LoginPane.this.connector.createPlayer(logName
						.getText());
				Vector<Room> rooms = LoginPane.this.connector.getAllRooms();

				Menu menu = new Menu(player, LoginPane.this.mainFrame, rooms);
				LoginPane.this.mainFrame.getLoginPane().setVisible(false);
				LoginPane.this.mainFrame.setMainMenu(menu);
				LoginPane.this.mainFrame.add(menu);

				LoginPane.this.mainFrame.getBroadcastListener().setRoomsPanel(
						menu.getRoomsPane());
				LoginPane.this.mainFrame.getBroadcastListener().setMainFrame(
						LoginPane.this.mainFrame);
				menu.getCreator().setBroadcastListener(
						LoginPane.this.mainFrame.getBroadcastListener());
				menu.getRoomsPane().setBroadCastListener(
						LoginPane.this.mainFrame.getBroadcastListener());

			}
		});

		cancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				System.exit(-1);
			}
		});

		this.setLayout(new GridBagLayout());
		JPanel pane = new JPanel(new GridBagLayout());
		pane.setBackground(new Color(255,255,255,10));

		GridBagConstraints paneC = new GridBagConstraints();
		paneC.weightx = 0.3;
		paneC.weighty = 0.2;
		paneC.gridx = 1;
		paneC.gridy = 1;

		// LAYOUT DESCRIPTION
		GridBagConstraints logC = new GridBagConstraints();
		logC.weightx = 0.3;
		logC.gridx = 2;
		logC.gridy = 1;
		pane.add(login, logC);
		GridBagConstraints logNameC = new GridBagConstraints();
		logNameC.weightx = 0.3;
		logNameC.gridx = 3;
		logNameC.gridy = 1;
		pane.add(logName, logNameC);
		GridBagConstraints logIPC = new GridBagConstraints();
		logC.weightx = 0.3;
		logC.gridx = 2;
		logC.gridy = 0;
		pane.add(ipName, logIPC);
		GridBagConstraints logNameIPC = new GridBagConstraints();
		logNameC.weightx = 0.3;
		logNameC.gridx = 3;
		logNameC.gridy = 0;
		pane.add(ipAddress, logNameIPC);		
		GridBagConstraints logStartC = new GridBagConstraints();
		logStartC.weightx = 0.3;
		logStartC.gridx = 3;
		logStartC.gridy = 2;
		pane.add(start, logStartC);
		GridBagConstraints logCloseC = new GridBagConstraints();
		logCloseC.weightx = 0.5;
		logCloseC.gridx = 2;
		logCloseC.gridy = 2;
		pane.add(cancel, logCloseC);
		this.add(pane, paneC);
	}

	public void paintComponent(Graphics g) {

		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(background, 0, 0, null);

	}
}
